package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"os"
	"strconv"
	"time"
)

const (
	MqRetryTimes = 2
	// NameServer   = "192.168.31.130:9876"
	NameServer = "139.198.168.66:40005"
)

// 获取生产者
func newProducer() rocketmq.Producer {
	addr := primitive.NamesrvAddr{NameServer}
	MqProducer, err := rocketmq.NewProducer(
		producer.WithNameServer(addr),
		producer.WithRetry(MqRetryTimes),
		producer.WithGroupName("go-producer"),
		producer.WithQueueSelector(producer.NewRandomQueueSelector()),
		producer.WithVIPChannel(true),
	)
	if err != nil {
		fmt.Println("init producer error", err.Error())
		os.Exit(1)
	}
	_ = MqProducer.Start()

	return MqProducer
}

func main() {
	pro := newProducer()
	count := 1
	for {
		str := "this is a msg data " + strconv.Itoa(count)
		message := primitive.NewMessage("item_test", []byte(str))
		sendResult, err := pro.SendSync(context.Background(), message)
		if err != nil {
			fmt.Println("send message error ", err.Error())
			return
		}
		fmt.Println("send message success", sendResult)
		time.Sleep(1 * 1e9)
	}

}
